<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加/修改指标</title>
</head>
<link rel="stylesheet" href="layui/css/layui.css" media="all">

<body>

<fieldset class="layui-elem-field layui-field-title"
          style="margin-top: 50px;">
    <legend>指标编辑</legend>
</fieldset>
<form  class="layui-form layui-form-pane" >

    <input type="hidden" name="indexId"  >


    <div class="layui-form-item">
        <label class="layui-form-label">指标名</label>
        <div class="layui-input-block">
            <input name="indexName" lay-verify="required"  autocomplete="off" placeholder="请输入指标名"
                   class="layui-input" type="text" >
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">父结点</label>
        <div class="layui-input-inline">
            <input name="parentId"  placeholder="请选择父结点"
                   autocomplete="off" class="layui-input" type="text" id="parentId">
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">指标权重</label>
        <div class="layui-input-inline">
            <input name="weight" lay-verify="required" placeholder="请输入权重"
                   autocomplete="off" class="layui-input" type="text">
        </div>
    </div>



    <div class="layui-form-item">
        <label class="layui-form-label">是否生效</label>
        <div class="layui-input-block">
            <input id="isEffective"   type="checkbox"   name="isEffective" lay-skin="switch" lay-filter="switchEffective" lay-text="生效|不生效">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">所属角色</label>
        <div class="layui-input-inline">
            <input name="roleId"  placeholder="请选择所属角色"
                   autocomplete="off" class="layui-input" type="text" id="roleId">
        </div>
    </div>





    <div class="layui-form-item">
        <button class="layui-btn" lay-submit="" lay-filter="form1" >提交</button>
    </div>

</form>

<script src="layui/layui.js"></script>
<script src="layui/layui.all.js"></script>
<script src="layui/lay/modules/tableSelect.js"></script>

<script type="text/javascript">

    var tableSelect = layui.tableSelect;

    /** 监听部门编号input框 */
    tableSelect.render({
        elem: '#parentId',
        searchKey: 'indexName',	//搜索输入框的name值 默认keyword
        searchPlaceholder: '指标名搜索',	//搜索输入框的提示文字 默认关键词搜索
        table: {
            url:'/evaluation/index/list',
            cols: [[
                { type: 'radio' },
                { field: 'indexId', title: '结点' },
                { field: 'indexName', title: '指标名' }
            ]],
            cellMinWidth : 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            height: 280,
            page : true,
            limits : [ 3, 5, 10 ],
        },
        done: function (elem, data) {
            var NEWJSON = []
            layui.each(data.data, function (index, item) {
                NEWJSON.push(item.indexId)
            })
            elem.val(NEWJSON.join(","))
        }
    });

    /**
     * 监听角色id
     */
    tableSelect.render({
        elem: '#roleId',
        searchKey: 'roleName',	//搜索输入框的name值 默认keyword
        searchPlaceholder: '角色搜索',	//搜索输入框的提示文字 默认关键词搜索
        table: {
            url:'/evaluation/role/list',
            cols: [[
                { type: 'radio' },
                { field: 'roleId', title: '角色id' },
                { field: 'roleName', title: '角色名' }
            ]],
            cellMinWidth : 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            height: 280,
            page : true,
            limits : [ 3, 5, 10 ],
        },
        done: function (elem, data) {
            var NEWJSON = []
            layui.each(data.data, function (index, item) {
                NEWJSON.push(item.roleId)
            })
            elem.val(NEWJSON.join(","))
        }
    });
</script>


<!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
<script type="text/javascript">

    layui.use(["form", "layer", "jquery"], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;



        /* function parseUrl(){
             var url=location.href;
             var i=url.indexOf('?');
             if(i==-1)return;
             var querystr=url.substr(i+1);
             var arr1=querystr.split('&');
             var arr2=new Object();
             for  (i in arr1){
                 var ta=arr1[i].split('=');
                 arr2[ta[0]]=ta[1];
             }
             return arr2;
         }
         var v = parseUrl();//解析所有参数
         // alert(v['userId']);//就是你要的结果
         var */

        var Ohref=window.location.href;
        var arrhref=Ohref.split("?indexId=");
        // alert(arrhref[1]);
        var indexId = arrhref[1];


        (function(){
            if (indexId !== undefined) {         //判断我是点的添加还是编辑
                /*此处是在编辑的时候根据userId在后台查询指标的全部信息并数据回显*/
                $.ajax({
                    url:"/evaluation/index/findByIndexId",
                    type:"get",
                    data:{indexId:indexId},
                    dataType:"json",
                    success:function(data){
                        var index = data.data;
                        // $("#userInfo").text(username + "    " + userNo);
                        // window.location.href ="login.html";
                        $("input[name='indexId']").val(index.indexId);
                        $("input[name='indexName']").val(index.indexName);
                        $("input[name='parentId']").val(index.parentId);
                        $("input[name='weight']").val(index.weight);
                        $("input[name='roleId']").val(index.roleId);
                        $("input[name='isEffective']").val(index.isEffective);
                        if (index.isEffective == 1){
                            $("input[name='isEffective']").attr('checked',true);
                        }else {
                            $("input[name='isEffective']").attr('checked',false);
                        }
                        form.render('checkbox'); //刷新checkbox选择框渲染

                    },
                    error:function(xmlHttpRequest,textStatus,errorThrown){
                        alert(textStatus+"出错！"+errorThrown);
                    }
                });
            }
        })();



        /*监听chebox复选框*/
        form.on('switch(switchEffective)', function(data){
            // console.log(data.elem); //得到checkbox原始DOM对象
            // console.log(data.elem.checked); //开关是否开启，true或者false
            // console.log(data.value); //开关value值，也可以通过data.elem.value得到
            // console.log(data.othis); //得到美化后的DOM对象
            /*if (this.checked) {
                $("input[name='isEffective']").val(1);
                // alert($("input[name='isEffective']").val());
                console.log(data.value);

            }else{
                // $("#isEffective").attr("value",0);
                $("input[name='isEffective']").val(0);
                // alert($("input[name='isEffective']").val());
                console.log(data.value)
                // $("input[name='isEffective']").val(0);
            }*/
            layer.msg('开关checked：'+ (this.checked ? 'true' : 'false'));
            this.checked ? $("input[name='isEffective']").val(1) : $("input[name='isEffective']").val(0);
            console.log($("input[name='isEffective']").val());     //如果为false，layui表单提交不会传我的值，会传null
       /*     $("#isEffective").attr("value",isEffectiveCopy);
            alert($("#isEffective").val());*/

        });



        form.on("submit(form1)", function (data) {
            /* layer.alert(($("#userId").val()==null || $("#userId").val()=='') ? "添加成功!" : "修改成功!", {icon: 1}); */
            $.post("/evaluation/index/save", data.field,function(d){
                if (d.code === 15 || d.code === 16){
                    layer.msg("很抱歉！操作失败了。",{icon: 5,time: 1500},function(){
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                    });
                }else if (d.code === 0) {
                    layer.msg("恭喜您！操作成功了。",{icon: 6,time: 1500},function(){
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                    });
                }
            },"json");


            // alert('添加指标成功！');


            return false;
        });



    });
</script>

</body>
</html>